package com.brunogb.pages.server.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.vraptor.annotations.Component;
import org.vraptor.annotations.In;
import org.vraptor.annotations.InterceptedBy;
import org.vraptor.annotations.Out;
import org.vraptor.annotations.Parameter;

import com.brunogb.pages.server.interceptors.UserInterceptor;
import com.brunogb.pages.server.model.Page;
import com.brunogb.pages.server.services.PageServiceImpl;

@Component
@InterceptedBy(UserInterceptor.class)
@SuppressWarnings("all")
public class PageAction {

	PageServiceImpl service;
	
	@Out private Page currentPage;
	
	@In private HttpServletRequest request;
	
	@In private HttpServletResponse response;
	
	@In private HttpSession session;
	
	private void init(){
		service = new PageServiceImpl();
	}
	
	private void setCurrentPage(Page p){
		session.setAttribute("currentPage", p);
	}
	
	private Page getCurrentPage(){
		Object o = session.getAttribute("currentPage");
		if (o != null){
			return (Page)o;
		}
		return null;
	}
	
	public void view(@Parameter(key="name") String name){
		init();
		Page p = service.findPage(name);
		setCurrentPage(p);
	}
	
	public void newPage(){
		init();
		Page p = new Page();
		service.save(p);
		setCurrentPage(p);
	}
	
	
}
